Imports System.IO

Public Class frmPrivateKey
    Private db As Database = Database.db

    Private Sub frmPrivateKey_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim key() As Byte = db.getMyPrivateKey
        frmNewUser.Close()
        Me.Hide()

        If Not key Is Nothing Then
            txtKey.Text = Convert.ToBase64String(key)
            Me.Text = db.getMyEmail

            Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\" _
                & db.getMyEmail & ".key"

            Dim response As Integer = MsgBox("Click YES to save your private key to My Document, or if you would like to save it somewhere else, click NO.", vbYesNo)
            If response = vbYes Then
                Try
                    File.WriteAllBytes(path, db.getMyPrivateKey)
                    Me.Hide()
                Catch ex As Exception
                    MsgBox(ex)
                End Try
            Else
                Me.Show()
                dlgSave.Filter = "Key file (.key)|*.key"
                dlgSave.FileName = db.getMyEmail & ".key"
                dlgSave.ShowDialog()
                Me.Hide()
                Try
                    File.WriteAllBytes(dlgSave.FileName, db.getMyPrivateKey)
                Catch ex As ArgumentNullException
                End Try
            End If
        Else
            Me.Show()
            Me.Text = "Please enter your private key"
        End If
        'Form1.Show()
    End Sub

    Private Sub btnSaveToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveToFile.Click
        dlgSave.Filter = "Key file (.key)|*.key"
        dlgSave.FileName = db.getMyEmail & ".key"
        dlgSave.ShowDialog()
        Try
            File.WriteAllBytes(dlgSave.FileName, db.getMyPrivateKey)
        Catch ex As ArgumentNullException
        End Try
        Me.SendToBack()
        'Form1.BringToFront()
        frmFileList.BringToFront()
        frmFileList.Focus()
    End Sub

    Private Sub btnLoadFromFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadFromFile.Click
        dlgOpen.Filter = "Key file (.key)|*.key"
        dlgOpen.ShowDialog()
        If Not IO.File.Exists(dlgOpen.FileName) Then Return
        Dim key As Byte() = File.ReadAllBytes(dlgOpen.FileName)
        db.setMyPrivateKey(key)
        txtKey.Text = Convert.ToBase64String(db.getMyPrivateKey)
        If Not db.validateMyPrivateKey Then
            'db.setMyPrivateKey(Nothing)
            MsgBox("The private key you provided does not match with the public key you have in the database!!", MsgBoxStyle.Critical, "Possible Security Breach")
        End If
        Me.Close()
    End Sub

    Private Sub btnValidate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValidate.Click
        If db.getMyId = -1 Then
            MsgBox("please log in to validate your key")
        ElseIf Not db.validateMyPrivateKey Then
            MsgBox("The private key you provided does not match with the public key you have in the database!!", MsgBoxStyle.Critical, "Possible Security Breach")
        End If
    End Sub

    Private Function getKeyFromTextBox() As Byte()
        Return Convert.FromBase64String(txtKey.Text.Trim)
    End Function

    Private Sub txtKey_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtKey.TextChanged
        'Form1.BringToFront()
        'frmFileList.BringToFront()
        'Me.Hide()
    End Sub
End Class